package dto

import (
	"encoding/json"
	"time"
)

type Ids []int64

func (i *Ids) UnmarshalBinary(data []byte) error {
	return json.Unmarshal(data, i)
}

type BaseInfo struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
}
type BaseInfos []BaseInfo

func (receiver BaseInfos) Unzip() ([]int64, []string) {
	var ids []int64
	var names []string
	for _, info := range receiver {
		ids = append(ids, info.ID)
		names = append(names, info.Name)
	}
	return ids, names
}

type UpdateBy[T any] struct {
	Name   string  `json:"name"`
	Ids    []int64 `json:"ids,omitempty"`
	Target T       `json:"target"`
}

type FindByIds struct {
	ID      int64   `json:"id,omitempty" form:"id"`
	Ids     []int64 `json:"ids,omitempty" form:"ids"`
	ItemIds []int64 `json:"itemIds,omitempty" form:"itemIds"`
}
type FindByIdType struct {
	ID   int64 `json:"id,omitempty"`
	Type int32 `json:"type,omitempty"`
	Data any   `json:"data,omitempty"`
}

type PageInfo struct {
	Page     int64 `json:"page" validate:"required,number"`     // 显示第几页
	PageSize int64 `json:"pageSize" validate:"required,number"` //一页显示多少
}

func (r PageInfo) Items(items []int64) []int64 {
	llen := int64(len(items))
	if r.Page*r.PageSize >= llen && (r.Page-1)*r.PageSize <= llen {
		items = items[r.PageSize*(r.Page-1):]
	} else if (r.Page-1)*r.PageSize < 0 && r.Page*r.PageSize >= 0 {
		items = items[:r.PageSize*(r.Page)]
	} else {
		items = items[r.PageSize*(r.Page-1) : r.PageSize*(r.Page)]
	}
	return items
}

type SearchWithPage[T any] struct {
	PageInfo PageInfo `json:"pageInfo"`
	Data     T        `json:"data"`
}

func (r PageInfo) Start() int64 {
	return r.PageSize * (r.Page - 1)
}

type SearchWithTime struct {
	StartTime time.Time `json:"startTime,omitempty" `
	EndTime   time.Time `json:"endTime,omitempty"`
}

type PageResult[T any] struct {
	Total int64 `json:"total"`
	Items []*T  `json:"items"`
}

type Response struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data"`
}
